﻿/*
 * @Description:新增房源页面js
 * @Author:yangpl
 * @Date:2014-09-05
 */
var PropertyAdd = new Class({
    //重写父类js中的自动完成方法
    Autocomplete: function () {
        var txtEstateName = $("#txtEstateName"), hidEstateKeyId = $("#hidEstateKeyId"), txtBuildingName = $("#txtBuildingName"), hidBuildingKeyId = $("#hidBuildingKeyId"), txtHouseNo = $("#txtHouseNo"), hidHouseKeyId = $("#hidHouseKeyId"), txtFloorVal = $("#txtFloorVal"), btnNextOperate = $("#btnNextOperate"), btnHouseNoRegex = $("#btnHouseNoRegex");
        //楼盘自动完成及赋值隐藏域
        txtEstateName.change(function () {
            $(this).removeClass("checkError").next("span[class=wrong]").remove();
            hidEstateKeyId.val("");
            txtBuildingName.val("");
            hidBuildingKeyId.val("");
            txtHouseNo.val("");
            btnHouseNoRegex.css("display", "none");
            hidHouseKeyId.val("");
            txtFloorVal.val("").attr("disabled", "disabled").attr("current-first-floor", "").removeClass("checkError").next("span[class=wrong]").remove();
            txtFloorVal.attr("building-type-code", "");
            txtBuildingName.attr("disabled", "disabled").removeClass("checkError").next("span[class=wrong]").remove();
            txtHouseNo.attr("disabled", "disabled").removeClass("checkError").next("span[class=wrong]").remove();
            if ($(this).next("span[class=wrong]").length <= 0 && !hidEstateKeyId.val() && other.trim(txtEstateName.val())) {
                //$(this).addClass("checkError").after('<span class="wrong">楼盘/期:必须从智能提示中选择！</span>');
                $(this).addClass("checkError").after(agencyVerify.greatErrorHtml($(this), '楼盘/期:必须从智能提示中选择！'));
                btnNextOperate.removeClass("btn-red").addClass("btn-gray").attr("disabled", "disabled");
            }

        });
        txtEstateName.autocomplete(function () {
            return "/property/estates-for-region-keyids-obtain-ajax?EstateName=" + escape(txtEstateName.val());
        }, {
            width: 260,
            max: 12,
            selectFirst: false,
            cacheLength: 0,
            formatItem: function (row) {
                return row.EstateFullName;
            },
            formatResult: function (row) {
                return row.EstateName;
            }
        }).result(function (a, b) {
            hidEstateKeyId.val(b.KeyId);

            $.AjaxAgency("/property/property-add-check-for-estate-ajax", { "KeyId": b.KeyId }, function (existData) {
                if (existData.indexOf("ok") > -1) {

                    txtEstateName.removeClass("checkError").next("span[class=wrong]").remove();

                    txtEstateName.attr("isstart", "true");
                } else if (existData.indexOf("#false:=:") > -1) {
                    if (txtEstateName.next("span[class=wrong]").length <= 0) {

                        //txtEstateName.addClass("checkError").after('<span class="wrong">' + (existData.split(":=:"))[1] + '</span>');
                        txtEstateName.addClass("checkError").after(agencyVerify.greatErrorHtml(txtEstateName, (existData.split(":=:"))[1]));
                    }
                    txtEstateName.attr("isstart", "false");
                } else {
                    //检测楼盘名异常
                    $.OKDialogAgency('<div class="tc">' + existData + '---请重新刷新页面，再操作！</div>');
                }
            });
            txtBuildingName.removeAttr("disabled");
            txtEstateName.removeClass("checkError").next("span[class=wrong]").remove();
        });


        //栋座信息
        txtBuildingName.change(function () {
            hidBuildingKeyId.val("");
            btnNextOperate.removeClass("btn-red").addClass("btn-gray").attr("disabled", "disabled");
            $(this).next("span[class=wrong]").remove();
            if ($(this).next("span[class=wrong]").length <= 0 && !hidBuildingKeyId.val() && $(this).val()) {
                //$(this).addClass("checkError").after('<span class="wrong">栋座单元:必须从智能提示选中！</span>');
                $(this).addClass("checkError").after(agencyVerify.greatErrorHtml($(this), '栋座单元:必须从智能提示选中！'));

            }

            //联动房间信息
            txtHouseNo.val("");
            txtHouseNo.attr("room-no-rule", "");
            btnHouseNoRegex.css("display", "none");
            hidHouseKeyId.val("");
            txtHouseNo.attr("disabled", "disabled").removeClass("checkError").next("span[class=wrong]").remove();

            //联动楼层
            txtFloorVal.val("").attr("disabled", "disabled").attr("current-first-floor", "").removeClass("checkError").next("span[class=wrong]").remove();
            txtFloorVal.attr("building-type-code", "");
        });

        txtBuildingName.autocomplete(function () {
            if (hidEstateKeyId.val()) {
                return "/estate/auto-match-building-brief?BuildingName=" + escape(txtBuildingName.val()) + '&EstateKeyId=' + hidEstateKeyId.val();// + "&BuildingTypeCode=10"
            }
            return '';
        }, {
            width: 260,
            max: 1000,
            cacheLength: 0,
            selectFirst: false,
            minChars:0,
            clickMode: true,
            formatItem: function (row) {
                return row.BuildingName;
            },
            formatResult: function (row) {
                return row.BuildingName;
            }
        }).result(function (a, b) {
            if (!b.IsBanAdd) {
                hidBuildingKeyId.val(b.KeyId);
                txtHouseNo.removeAttr("disabled");
                txtBuildingName.removeClass("checkError").next("span[class=wrong]").remove();
                txtFloorVal.attr("building-type-code", b.BuildingTypeCode);
                if (b.FirstFloor) {
                    txtFloorVal.attr("current-first-floor", b.FirstFloor);

                } else {
                    //if (b.BuildingTypeCode == "40") {
                    //} else {
                    //    txtFloorVal.attr("current-first-floor", "").after(agencyVerify.greatErrorHtml(txtFloorVal.attr("current-first-floor", ""), '楼层没有起始层请到楼盘字典设置！'));
                    //}

                }

                //txtHouseNo.attr("room-no-rule", b.RoomNoRuleItemValue);
                //btnHouseNoRegex.html("");
                //if (b.BuildingTypeCode != "40") {
                //    btnHouseNoRegex.html(btnHouseNoRegex.attr("showval") + other.houseRegex(b.RoomNoRuleCode)).css("display", "");
                //}

                if (b.BuildingTypeCode == "10") {
                    txtHouseNo.attr("room-no-rule", b.RoomNoRuleItemValue);
                    btnHouseNoRegex.html("");
                    btnHouseNoRegex.html(btnHouseNoRegex.attr("showval") + other.houseRegex(b.RoomNoRuleCode)).css("display", "");
                }
                else {
                    txtHouseNo.attr("room-no-rule", "");
                }


            } else {
                
                if ($("#hidPropertyNoLockPermisstion").val() == "1") {
                    hidBuildingKeyId.val(b.KeyId);
                    txtHouseNo.removeAttr("disabled");
                    txtBuildingName.removeClass("checkError").next("span[class=wrong]").remove();
                    txtFloorVal.attr("building-type-code", b.BuildingTypeCode);
                    if (b.FirstFloor) {
                        txtFloorVal.attr("current-first-floor", b.FirstFloor);

                    } else {
                        

                    }

                    //txtHouseNo.attr("room-no-rule", b.RoomNoRuleItemValue);
                    //btnHouseNoRegex.html("");
                    //if (b.BuildingTypeCode != "40") {
                    //    btnHouseNoRegex.html(btnHouseNoRegex.attr("showval") + other.houseRegex(b.RoomNoRuleCode)).css("display", "");
                    //}
                    if (b.BuildingTypeCode == "10") {
                        txtHouseNo.attr("room-no-rule", b.RoomNoRuleItemValue);
                        btnHouseNoRegex.html("");
                        btnHouseNoRegex.html(btnHouseNoRegex.attr("showval") + other.houseRegex(b.RoomNoRuleCode)).css("display", "");
                    }
                    else {
                        txtHouseNo.attr("room-no-rule", "");
                    }
                }
                else {
                    txtBuildingName.removeClass("checkError").next("span[class=wrong]").remove();
                    txtBuildingName.addClass("checkError").after(agencyVerify.greatErrorHtml(txtBuildingName, '当前栋座单元已锁定，无法新增，请联系后勤人员！'));
                    btnHouseNoRegex.css("display", "none");
                }

            }

        });


        //房间信息
        txtHouseNo.focus(function() {
            txtHouseNo.removeClass("checkError").next("span[class=wrong]").remove();
        });
        txtHouseNo.change(function () {
            txtFloorVal.val("").removeClass("checkError").next("span[class=wrong]").remove();
            if (txtHouseNo.val() && txtEstateName.attr("isstart") == "true") {

            } else {
                btnNextOperate.attr("disabled", "disabled").attr("class", "btn-gray");
            }


        });
        //自动完成房间填充
        txtHouseNo.autocomplete(function () {
            return "/estate/auto-match-house-no?BuildingKeyId=" + hidBuildingKeyId.val() + "&EstateKeyId=" + hidEstateKeyId.val() + "&HouseNo=" + escape(txtHouseNo.val()) + "&IsExcludeProperty=true";
        }, {
            width: 260,
            max: 12,
            cacheLength: 0,
            selectFirst: false,
            formatItem: function (row) {

                return row.ItemText;

            },
            formatResult: function (row) {
                return row.ItemText;
            }
        }).result(function (a, b) {
            hidHouseKeyId.val(b.ItemValue);
            txtHouseNo.blur();
        });

        txtHouseNo.focus(function () {
            if (txtHouseNo.next("span[class=wrong]").html() && txtHouseNo.next("span[class=wrong]").html().indexOf("房号已存在！查看此房源") > -1) {
                txtHouseNo.removeClass("checkError").next("span[class=wrong]").remove();
            }
        }).blur(function () {
            var patrn = new RegExp(txtHouseNo.attr("room-no-rule"));
            if (!patrn.test(txtHouseNo.val())) {
                if (!$(this).val()) {
                    $(this).next("span[class=wrong]").remove();
                    //txtHouseNo.after('<span class="wrong">该值不能为空</span>');
                    txtHouseNo.after(agencyVerify.greatErrorHtml(txtHouseNo, '此项必填'));
                    //} else if ($(this).next("span[class=wrong]").length <= 0 && txtFloorVal.attr("building-type-code") != "40") {
                } else if ($(this).next("span[class=wrong]").length <= 0 && txtFloorVal.attr("building-type-code") == "10") {
                    //txtHouseNo.addClass("checkError").after('<span class="wrong">房号规则不正确！</span>');
                    txtHouseNo.addClass("checkError").after(agencyVerify.greatErrorHtml(txtHouseNo, '房号规则不正确！'));
                    $("#btnNextOperate").removeClass("btn-red").addClass("btn-gray").attr("disabled", "disabled");
                }
            } else {
                if (txtHouseNo.next("span[class=wrong]").html() == "房号规则不正确！") {
                    txtHouseNo.removeClass("checkError").next("span[class=wrong]").remove();
                }

                //房号规则正确，检测房号是否存在
                var reqData = {
                    "EstateKeyId": hidEstateKeyId.val(),
                    "BuildingKeyId": hidBuildingKeyId.val(),
                    "HouseNo": txtHouseNo.val()
                };
                $.AjaxAgency("/property/Property-house-number-check-ajax", reqData, function (respData) {
                    if (respData.indexOf("#error:") != -1) {
                    } else if (respData == "noexist") {
                        if (txtHouseNo.next("span[class=wrong]").html() && txtHouseNo.next("span[class=wrong]").html().indexOf("房号已存在！查看此房源") > -1) {
                            txtHouseNo.removeClass("checkError").next("span[class=wrong]").remove();

                        }
                        txtFloorVal.removeAttr("disabled"); 
                    } else {
                       
                        if (txtHouseNo.next("span[class=wrong]").length <= 0) {
                            //txtHouseNo.addClass("checkError").after('<span class="wrong"><a href="/property/details?keyId=' + respData + '" target="_blank">房号已存在！查看此房源</a></span>');
                            txtHouseNo.addClass("checkError").after(agencyVerify.greatErrorHtml(txtHouseNo, '<a href="/property/details?keyId=' + respData + '" target="_blank">房号已存在！查看此房源</a>'));
                            btnNextOperate.removeClass("btn-red").addClass("btn-gray").attr("disabled", "disabled");
                        }

                    }
                });
                //房号规则正确，检测房号是否存在End
            }
        });



        //将子类对象转入父类执行父类的方法
        //this.base.Autocomplete.apply(this, arguments);
    },
    //普通事件绑定
    EventsBind: function () {
        agencyVerify.zhengFuIntInput("#txtFloorVal");
        $("#txtFloorVal").bind("keyup input paste blur", function () {
            var tempHint = "楼层没有起始层请到楼盘字典设置！";
            var tempVal = $(this).attr("current-first-floor");
            if (tempVal == "" && $(this).attr("building-type-code") != "40") {
                if ($(this).next(".wrong").length <= 0) {
                    // $(this).addClass("checkError").after('<span class="wrong">' + tempHint + '</span>');
                }
            } else {
                if ($(this).attr("building-type-code") != "40") {
                    if ((!$(this).val().match(/^(-|\+)?\d+$/)) || ($(this).val() == 0 || $(this).val().indexOf('0')==0)) {
                        $(this).removeClass("checkError").next(".wrong").remove();                      
                        $(this).addClass("checkError").after(agencyVerify.greatErrorHtml($(this), '楼层必须是非零整数！'));
                        $("#btnNextOperate").removeClass("btn-red").addClass("btn-gray").attr("disabled", "disabled");

                    } else if (parseInt($(this).val(),10) < parseInt(tempVal,10)) {
                        $(this).removeClass("checkError").next(".wrong").remove();
                        $(this).addClass("checkError").after(agencyVerify.greatErrorHtml($(this), '楼层不能小于' + tempVal));
                        $("#btnNextOperate").removeClass("btn-red").addClass("btn-gray").attr("disabled", "disabled");
                    } else {
                        $(this).removeClass("checkError").next(".wrong").remove();
                        $("#btnNextOperate").removeClass("btn-gray").addClass("btn-red").removeAttr("disabled");
                    }
                } else {
                    if ((!$(this).val().match(/^(-|\+)?\d+$/))) {
                        $(this).removeClass("checkError").next(".wrong").remove();
                        $(this).addClass("checkError").after(agencyVerify.greatErrorHtml($(this), '楼层必须是整数！'));
                        $("#btnNextOperate").removeClass("btn-red").addClass("btn-gray").attr("disabled", "disabled");

                    } else {
                        $(this).removeClass("checkError").next(".wrong").remove();
                        $("#btnNextOperate").removeClass("btn-gray").addClass("btn-red").removeAttr("disabled");
                    }
                    $("#btnNextOperate").removeClass("btn-red").addClass("btn-gray").attr("disabled", "disabled");
                }

            }
            

            if (($(this).val() == 0)) {
                $(this).removeClass("checkError").next(".wrong").remove();
                $(this).addClass("checkError").after(agencyVerify.greatErrorHtml($(this), '楼层必须是非零整数！'));
                $("#btnNextOperate").removeClass("btn-red").addClass("btn-gray").attr("disabled", "disabled");

            }

            if ($(this).val() == "" && $(this).next(".wrong").html("楼层必须是整数！")) {
                $(this).removeClass("checkError").next(".wrong").remove();
                $("#btnNextOperate").removeClass("btn-red").addClass("btn-gray").attr("disabled", "disabled");
            }

            if ($("#frameInfo").find(".wrong").length <= 0) {
                $("#btnNextOperate").removeClass("btn-gray").addClass("btn-red").removeAttr("disabled");
            }
        }).focus(function () {
            $(this).removeClass("checkError").next("span[class=wrong]").remove();
            $("#btnNextOperate").removeClass("btn-red").addClass("btn-gray").attr("disabled", "disabled");
        });
        EmployeePrompt.getDepartAndEmployeeByInput("txtDepartmentId", "hidDepartmentId", "txtPropertyChiefKeyId", "hidUserId", false, true, 1, "", "", true,true);
        for (var i = 0; i < PropertyStatusFilter.PropertyStatus.length; i++) {
            if (PropertyStatusFilter.PropertyStatus[i].Code == "100") {
                $("#hidPropertyStatusKeyId").val(PropertyStatusFilter.PropertyStatus[i].Value);
                break;
            }
        }
        $("#txtPropertyChiefKeyId").blur(function () {
            //if (!$("#hidUserId").val()) {
            //    $("#txtPropertyChiefKeyId").val("");
            //}
            if ($(this).val() == "") {
                $(this).addClass("checkError");
                $(this).after(agencyVerify.greatErrorHtml($(this), '此项必填'))
            } else {
                $(this).removeClass("checkError");
                $(this).parent().find(".wrong").remove();
            }
        });

        $("#btnNextOperate").click(function () {
            if ((!(/^(-\d|\+\d)?\d*$/).test($("#txtFloorVal").val())) || $("#txtFloorVal").val()=="") {
                $("#txtFloorVal").removeClass("checkError").next(".wrong").remove();
                $("#txtFloorVal").addClass("checkError").after(agencyVerify.greatErrorHtml($("#txtFloorVal"), '楼层必须是整数！'));
                $("#btnNextOperate").removeClass("btn-red").addClass("btn-gray").attr("disabled", "disabled");

            }
            //if ($("#txtHouseNo").next("span[class=wrong]").length <= 0 && $("#txtFloorVal").attr("building-type-code") != "40") {
            if ($("#txtHouseNo").next("span[class=wrong]").length <= 0 && $("#txtFloorVal").attr("building-type-code") == "10") {
                var patrn = new RegExp($("#txtHouseNo").attr("room-no-rule"));
                if (!patrn.test($("#txtHouseNo").val())) {
                    $("#txtHouseNo").addClass("checkError").after(agencyVerify.greatErrorHtml($("#txtHouseNo"), '房号规则不正确！'));

                }
            }

            if ($("#frameInfo").find(".wrong").length <= 0 && $("#txtEstateName").attr("isstart") == "true") {
                $("#divMainSubContent").show();
                $("#txtEstateName").attr("readonly", "readonly");
                $("#txtBuildingName").attr("readonly", "readonly");
                $("#txtHouseNo").attr("readonly", "readonly");
                $("#txtFloorVal").attr("readonly", "readonly");
                $(this).remove();
            } else {
                $("#txtEstateName").attr("isstart", "true");
                $("#btnNextOperate").removeClass("btn-red").addClass("btn-gray").attr("disabled", "disabled");
            }

        });
        this.base.EventsBind.apply(this, arguments);
    }
}, PropertyAddandEdit);
